package com.yueqian.epidemic.controller;

import com.yueqian.epidemic.beans.*;
import com.yueqian.epidemic.service.EpidemicService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 2020-03-05
 * Loongson
 **/
@Controller
@RequestMapping("/epidemicData")
public class EpidemicController {

    private Logger logger = Logger.getLogger(EpidemicController.class);

    @Autowired
    private EpidemicService epidemicService;


    @PostMapping("/ajax/input")
    @ResponseBody
    public AjaxResponseInfo inputData(@RequestBody DailyEpidemicInfo dailyEpidemicInfo, HttpSession session) {
        logger.debug(dailyEpidemicInfo);
        AjaxResponseInfo info = new AjaxResponseInfo();
        //得到用户，判断是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute("loginedUser");
        //说明还没有登录，没有权限
        if (userInfo == null) {
            info.setCode(-2);
            info.setMsg("您还没有登录呢！");
        } else {
            //往数据库里面存储并且返回新的一批省份名单
            List<ProvinceInfo> provinceInfos = epidemicService.saveData(dailyEpidemicInfo, userInfo.getUserId());
            info.setData(provinceInfos);
        }
        return info;
    }

    @GetMapping("/ajax/lastestData")
    @ResponseBody
    public AjaxResponseInfo lastestData(){
        AjaxResponseInfo info = new AjaxResponseInfo();
        List<EpidemicDetailInfo> list = this.epidemicService.findLastestData();
        info.setData(list);
        return info;
    }

}
